import{ defineStore } from 'pinia'
import type { ToastOptions } from'wot-design-uni/components/wd-toast/types'
import { getCurrentPath, merge } from "@/utils"

interface GlobalToast{
  toastOptions:ToastOptions
  currentPage:string
  currentKey: number
}

const defaultOptions: ToastOptions= {
  duration: 2000,
  show:false,
}

export const useGlobalToast = defineStore('global-toast', {
  state: (): GlobalToast=>({
    toastOptions: defaultOptions,
    currentPage:'',
    currentKey: 0
  }),
  actions: {
    // 显示 Toast
    show(option:ToastOptions|string) {
      // @ts-ignore
      this.currentPage = getCurrentPath()
      const options = merge(
        defaultOptions,
        typeof option ==='string'? {msg: option } : option
      ) as ToastOptions

      this.toastOptions = merge(options, {
        show:true,
        position: options.position||'middle',
      }) as ToastOptions
      this.currentKey++;
    },

    // 成功提示
    success(option:ToastOptions|string) {
      this.show(merge({
        iconName:'success',
        // duration:1500,
      },typeof option ==='string'? {msg: option } : option) as ToastOptions)
    },

    // 错误提示
    error(option:ToastOptions|string) {
      this.show(merge({
        iconName:'error',
        direction:'vertical',
      },typeof option ==='string'? {msg: option } : option) as ToastOptions)
    },

    // 信息提示
    info(option:ToastOptions|string) {
      this.show(merge({
        iconName:'info',
      },typeof option ==='string'? {msg: option } : option) as ToastOptions)
    },

    // 警告提示
    warning(option:ToastOptions|string) {
      this.show(merge({
        iconName:'warning',
      },typeof option ==='string'? {msg: option } : option) as ToastOptions)
    },

    // 关闭 Toast
    close() {
      this.toastOptions= defaultOptions
      this.currentPage = ''
      this.currentKey = -1;
    },
  },
})

export declare type ToastUtil = ReturnType<typeof useGlobalToast>